//
// Surface.cpp for math interface in /home/druart_p/Work/math/my-math-epi/203
//
// Made by pierrick druart
// Login   <druart_p@epitech.net>
//
// Started on  Mon Mar 19 16:00:57 2012 pierrick druart
// Last update Tue Apr 10 18:51:38 2012 pierrick druart
//

#include	"Surface.hh"

/******************************/
/* Constructors & Destructors */
/******************************/

Surface::Surface(char **av, std::list<double> list)
{
  QPalette		palette(Surface::palette());
  std::istringstream	iss(av[1]);
  int			opt;

  iss >> opt;
  this->length = LENGTH;
  this->heigth = HEIGTH;
  this->av = av;
  if (opt == 2)
    this->echelle = av[2];
  else
    this->echelle = "0";
  this->_list = list;
  setWindowTitle("Math 203Poisson");
  setFixedSize(this->length, this->heigth);
  palette.setColor(backgroundRole(), Qt::black);
  setPalette(palette);
}

Surface::Surface(char **av, char *str, std::list<double> list, std::list<double> list2)
{
  QPalette		palette(Surface::palette());

  this->length = LENGTH;
  this->heigth = HEIGTH;
  this->av = av;
  this->echelle = str;
  this->_list = list;
  this->_list2 = list2;
  setWindowTitle("Math 203Poisson");
  setFixedSize(this->length, this->heigth);
  palette.setColor(backgroundRole(), Qt::black);
  setPalette(palette);
}

Surface::~Surface() {}

/*********************/
/*  Painter Event    */
/*********************/

void		Surface::paintEvent(QPaintEvent *)
{
  QPainter		painter(this);
  std::istringstream	iss(this->av[1]);
  int			opt;
  int			i;
  int			j;

  iss >> opt;
  painter.setBrush(Qt::white);
  painter.setPen(Qt::white);
  if (opt != 2 && opt != 4)
    this->echelle = "100";
  for (i = 0; i < this->length; i++)
    for (j = 0; j < this->heigth; j++)
      {
	if (i == 50 || j == (this->heigth - 50))
	  painter.drawPoint(i, j);
      }
  painter.drawText(40, 15, "1");
  painter.drawText(40, (this->heigth - 35), "0");
  painter.drawText((this->length - 30), (this->heigth - 30), this->echelle);
  painter.drawLine((this->length - 9), (this->heigth - 55), (this->length - 9), (this->heigth - 45));
  line(&painter, opt);
}

// TRACER LINE

void		Surface::line(QPainter *painter, int opt)
{
  std::list<double>::iterator it;
  std::list<double>::iterator it2;
  std::istringstream	iss2(this->echelle);
  int			size;
  iss2 >> size;
  int			axe_y = this->heigth - 51;
  int			axe_x = 50;
  int			echelle = ((this->length - 50) / (size + 1));

  painter->setPen(Qt::red);
  for (it = this->_list.begin(); it != this->_list.end(); ++it)
    {
      axe_x = axe_x + echelle;
      painter->drawLine(axe_x, axe_y, axe_x, axe_y - (axe_y - ((1 - *it) * axe_y)));
    }
  if (!this->_list2.empty())
    {
      int		axe_x = 55;

      painter->setPen(Qt::green);
      for (it2 = this->_list2.begin(); it2 != this->_list2.end(); ++it2)
	{
	  axe_x = axe_x + echelle;
	  painter->drawLine(axe_x, axe_y, axe_x, axe_y - (axe_y - ((1 - *it2) * axe_y)));
	}
      painter->drawText(this->length - 180, 15, "Loi de Poisson p=10");
      painter->setPen(Qt::red);
      painter->drawText(this->length - 180, 30, "Loi Binomiale n=100 p=0.1");
      painter->setPen(Qt::yellow);
      painter->drawText((this->length / 2) - 90, this->heigth - 15, "Similitude loi Binomiale et loi de Poisson");
    }
  else if (opt == 2)
    {
      painter->setPen(Qt::yellow);
      painter->drawText((this->length / 2) - 60, this->heigth - 15, "Loi Binomiale avec n=");
      painter->drawText((this->length / 2) + 65, this->heigth - 15, this->av[2]);
      painter->drawText((this->length / 2) + 90, this->heigth - 15, " et p=");
      painter->drawText((this->length / 2) + 130, this->heigth - 15, this->av[3]);
    }
  else
    {
      painter->setPen(Qt::yellow);
      painter->drawText((this->length / 2) - 60, this->heigth - 15, "Loi de Poisson avec n=");
      painter->drawText((this->length / 2) + 75, this->heigth - 15, this->av[2]);
    }
}
